<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/27
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
/**
 * 计算价格订单1🍀
 */
class CountPriceOrder1 extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    /**
     * 计算价格订单1
     * 
     * @param string $load_status 装车类型:1=整车,2=零担
     * @param int $car2_id 车2id
     * @param integer $long 货物长
     * @param float $wide 货物宽
     * @param integer $high 货物高
     * @param integer $km 2地距离km
     * @param integer $t 货物重量吨
     * @return void
     */
    public function CalculatePriceOrder1($load_status='1',$long =null,$wide =null,$high =null,$km  =null,$t=null,$car2_id=null)
    {
        //load_status装车类型:1=整车,2=零担
        switch ($load_status) {
            case '1':
                $res = $this->Order1WholePrice($km  ,$car2_id,true);
                break;

            case '2':
                $res = $this->Order1BulkloadPrice($long ,$wide ,$high ,$km  ,$t ,true);
                break;
            
            default:
                # code...
                break;
        }
        $this->success('ok',$res);
    }


    /**
     * 计算整车价格
     * 
     * @param int $car2_id 车2id
     * @param integer $km 2地距离km
     * @return void
     */
    public function Order1WholePrice($km,$car2_id,$self=false)
    {
        $car2 = Db::name('car2')->find($car2_id);
        if(!$car2){
            $this->error('没有该车辆');
        }
        if($km<=51){
            $res = $car2['price1'];
        }elseif($km>51 && $km<=81){
            $res = $car2['price2'];
        }elseif($km>81 && $km<=121){
            $res = $car2['price3'];
        }elseif($km>121){

            $res = $km * $car2['price4'];
        }

        if($self)
        return $res;
        $this->success('ok',$res);
    }





    /**
     * 计算零担散货价格（长宽高 距离 吨）
     * 
     * 
     *
     * @param integer $long 货物长
     * @param float $wide 货物宽
     * @param integer $high 货物高
     * @param integer $km 2地距离km
     * @param integer $t 货物重量吨
     * @return void
     */
    public function Order1BulkloadPrice($long ,$wide ,$high ,$km  ,$t,$self=false)
    {
        $res = $this->Long_Wide_High_GetBulkloadPrice(null,$long ,$wide ,$high ,$km  ,$t );
        if($self)
        return $res;
        $this->success('ok',$res);
    }




    /**
     * 订单获取零担价格_订单号
     * @ApiInternal()
     * @param string $order 订单
     * @return void
     */
    public function OrderGetBulkloadPrice($order){
        if(!is_array($order)){
            $order = Db::name('order1')->where('order',$order)->find();
        }
        return $this->Long_Wide_High_GetBulkloadPrice($order);
    }    

    /**
     * 长宽高获取价格
     * 
     * @ApiInternal()
     *
     * @param array $order 数组
     * @param integer $long 货物长
     * @param float $wide 货物宽
     * @param integer $high 货物高
     * @param integer $km 2地距离km
     * @param integer $t 货物重量吨
     * @return void
     */
    public function Long_Wide_High_GetBulkloadPrice($order=null,$long = 3,$wide = 1.5,$high = 2,$km = 300,$t = 1)
    {
        if(!$order){
            $order['long'] = $long;
            $order['wide'] = $wide;
            $order['high'] = $high;
            $order['km'] = $km;
            $order['t'] = $t;
        }



        $num_arr[] = $order['long'];
        $num_arr[] = $order['wide'];
        $num_arr[] = $order['high'];
        $long=0;
        foreach($num_arr as $key=>$val){
            if($long<$val){
                $long = $val;                
            }
        }

        $wide_high = $num_arr;//定义宽高
        foreach($wide_high as $key=>$val){
            if($long==$val){
                unset($wide_high[$key]);
                break;           
            }
        }

        $wide_high = array_merge($wide_high);
         
        //宽高
        if($wide_high[0]>$wide_high[1]){
                //超
            if($wide_high[0] >= 2 && $wide_high[1] >= 1.5){
                $price = $this->GetBulkload2Price($order['km'],$order['t']);
            }else{
                //不超宽高
                $price = $this->GetBulkload1Price($order['km'],$order['t']);
            }

        }else{
            //超
            if($wide_high[1] >= 2 && $wide_high[0] >= 1.5){
                $price = $this->GetBulkload2Price($order['km'],$order['t']);
            }else{
                //不超宽高
                $price = $this->GetBulkload1Price($order['km'],$order['t']);
            }
        }

        return $price;
        halt($price);
     

        // $wide_high = array_merge(array_diff($num_arr, [$long]));
        // halt($wide_high);

        // $res = Db::name('bulkload')->where('max',$km)->find();
        // if(!$res){
        //     $res = Db::name('bulkload')->where('min','<',$km)->where('max','>',$km)->find();
        //     if(!$res){
        //         $this->error('请输入正确的公里数');
        //     }
        // }
        // return $res;
    }

    /**
     * 获取零担超高1.5超宽2米
     * @ApiInternal()
     * 
     * @param float $km 公里
     * @param float $t 吨
     * @return void
     */
    public function GetBulkload2Price($km,$t=1)
    {
        $res = Db::name('bulkload2')->where('max',$km)->find();
        if(!$res){
            $res = Db::name('bulkload2')->where('min','<',$km)->where('max','>',$km)->find();
            if(!$res){
                $this->error('请输入正确的公里数');
            }
        }
        return round($res['price'] * $km * ceil($t),2);//单价*公里*吨
        return $res;
    }

    /**
     * 获取零担不超
     * @ApiInternal()
     * 
     * @param float $km 公里
     * @param float $t 吨
     * @return void
     */
    public function GetBulkload1Price($km,$t=1)
    {
        $res = Db::name('bulkload')->where('max',$km)->find();
        if(!$res){
            $res = Db::name('bulkload')->where('min','<',$km)->where('max','>',$km)->find();
            if(!$res){
                $this->error('请输入正确的公里数');
            }
        }
        return round($res['price'] * $km * ceil($t),2);//单价*公里*吨
        return $res;
    }





}
